import Vue from 'vue'
import VueRouter from 'vue-router'

import {Message} from "element-ui";
import Login from "@/components/login/Login.vue";
import Home from "@/components/common/Home.vue";
import User from "@/views/user/User.vue";
import UserList from "@/views/user/UserList.vue";
import CarTableData from "@/views/table/CarTableData.vue";
import View1 from "@/views/view/View1.vue";
import View2 from "@/views/view/View2.vue";
import View3 from "@/views/view/View3.vue";
import ViewHome from "@/views/view/ViewHome.vue";
import View4 from "@/views/view/View4.vue";
import Yuce from "@/views/yuce/Yuce.vue";


Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: "/home",
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
        redirect: "/viewHome",
        children: [
            {
                path: "/viewHome",
                component: ViewHome
            },
            {
                path: "/carData",
                component: CarTableData
            },
            {
                path: "/view1",
                component: View1
            },
            {
                path: "/view2",
                component: View2
            },
            {
                path: "/view4",
                component: View4
            },
            {
                path: "/view3",
                component: View3
            },

            {
                path: "/user",
                component: User
            },
            {
                path: "/userList",
                component: UserList
            },
            {
                path: "/yuce",
                component: Yuce
            },

        ]
    },
    {
        path: "/login",
        name: "login",
        component: Login
    },
    {
        path: '*',
        name: '/404',
        component: () => import('../views/error/404.vue')
    }
]


const router = new VueRouter({
    routes,
    scrollBehavior: () => ({y: 0}),
})

router.beforeEach((to, from, next) => {
    const userInfo = JSON.parse(localStorage.getItem("userInfo"))
    if (to.path === '/login') {
        next()
    } else if (userInfo !== null && Date.now() < userInfo.expire) {
        next()
    } else {
        Message.error("请登陆账号")
        localStorage.removeItem("userInfo")
        next("/login")
    }
})
export default router
